home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / TransSkel Pascal 2.5 / TransSkel / NewStuff ƒ / DialogUtils.p next >
Encoding:
Text File  |  1994-12-24  |  7.9 KB  |  303 lines  |  [TEXT/PJMM]

  1. {Some selected dialog utilities from my private library}
  2. {Note: These are considerably safer than most similar dialog units I have seen, since they check}
  3. {the item kind rather than just blindly assuming that the programmer know what he/she is doing.}
  4.  
  5. unit DialogUtils;
  6.  
  7. interface
  8.  
  9. {$IFC UNDEFINED THINK_PASCAL}
  10.     uses
  11.         Memtypes, Quickdraw, OSIntf, ToolIntf, PackIntf;
  12. {$ENDC}
  13.  
  14.     procedure HighlightDItem (theDialog: DialogPtr; itemNo: integer);
  15.     procedure HiliteDItem (theDialog: DialogPtr; itemNo: integer);
  16.     procedure EnableDItem (theDialog: DialogPtr; itemNo: integer);
  17.     procedure DisableDItem (theDialog: DialogPtr; itemNo: integer);
  18.     function TestDItemEnable (theDialog: DialogPtr; itemNo: integer): boolean;
  19.     procedure HideDItem (theDialog: DialogPtr; itemNo: integer);
  20.     procedure ShowDItem (theDialog: DialogPtr; itemNo: integer);
  21.     procedure UpdDialogRect (theDialog: DialogPtr; theRect: Rect);
  22.     procedure SetTextDItem (theDialog: DialogPtr; itemNo: integer; theString: Str255);
  23.     function GetTextDItem (theDialog: DialogPtr; itemNo: integer): Str255;
  24.     procedure SetBooleanDItem (theDialog: DialogPtr; itemNo: integer; theBoolean: Boolean);
  25.     function GetBooleanDItem (theDialog: DialogPtr; itemNo: integer): Boolean;
  26.     procedure ToggleBooleanDItem (theDialog: DialogPtr; itemNo: integer);
  27.     function GetDItemBox (theDialog: DialogPtr; itemNo: integer): Rect;
  28.     function StdFilter (theDialog: DialogPtr; var theEvent: EventRecord; var itemHit: integer): boolean;
  29.     function MyFindControl (plats: Point; theDialog: DialogPtr; var theItem: ControlHandle): integer;
  30.  
  31. implementation
  32.  
  33.  
  34.     procedure HighlightDItem (theDialog: DialogPtr; itemNo: integer);
  35.         var
  36.             kind: integer;
  37.             item: ControlHandle;
  38.             box: Rect;
  39.     begin
  40.         GetDItem(theDialog, itemNo, kind, Handle(item), box);
  41.         if item = nil then
  42.             SysBeep(1)
  43.         else
  44.             HiliteControl(ControlHandle(item), 1); {10 för button, 11 för radio?}
  45.     end;
  46.  
  47.     procedure HiliteDItem (theDialog: DialogPtr; itemNo: integer);
  48.         var
  49.             kind: integer;
  50.             item: ControlHandle;
  51.             box: Rect;
  52.     begin
  53.         GetDItem(theDialog, itemNo, kind, Handle(item), box);
  54.         if item = nil then
  55.             SysBeep(1)
  56.         else
  57.             HiliteControl(ControlHandle(item), 1); {10 för button, 11 för radio?}
  58.     end;
  59.  
  60.     procedure EnableDItem (theDialog: DialogPtr; itemNo: integer);
  61.         var
  62.             kind, realkind: integer;
  63.             item: Handle;
  64.             box: Rect;
  65.     begin
  66.         GetDItem(theDialog, itemNo, kind, item, box);
  67.         if item = nil then
  68.             SysBeep(1)
  69.         else
  70.             begin
  71.                 realkind := BitAnd(kind, 127);
  72.                 case realkind of
  73.                     8, 16: {statText, editText}
  74.                         SetDItem(theDialog, itemNo, realkind, item, box); {meningslöst?}
  75.                     0, 1, 2, 4, 5, 6: {button, checkbox, radio - men vad är 4?}
  76.                         HiliteControl(ControlHandle(item), 0);
  77.                     otherwise
  78.                         SysBeep(1);
  79.                 end; {case}
  80.             end;
  81.     end;
  82.  
  83.     procedure DisableDItem (theDialog: DialogPtr; itemNo: integer);
  84.         var
  85.             kind, realkind: integer;
  86.             item: Handle;
  87.             box: Rect;
  88.     begin
  89.         GetDItem(theDialog, itemNo, kind, item, box);
  90.         if item = nil then
  91.             SysBeep(1)
  92.         else
  93.             begin
  94.                 realkind := BitAnd(kind, 127);
  95.                 case realkind of
  96.                     8, 16: {statText, editText}
  97.                         SetDItem(theDialog, itemNo, kind + itemDisable, item, box); {meningslöst?}
  98.                     0, 1, 2, 4, 5, 6: {button, checkbox, radio - men vad är 4?}
  99.                         HiliteControl(ControlHandle(item), 255);
  100.                     otherwise
  101.                         SysBeep(1);
  102.                 end; {case}
  103.             end;
  104.     end;
  105.  
  106.     function TestDItemEnable (theDialog: DialogPtr; itemNo: integer): boolean;
  107.         var
  108.             kind: integer;
  109.             item: ControlHandle;
  110.             box: Rect;
  111.     begin
  112.         GetDItem(theDialog, itemNo, kind, Handle(item), box);
  113.         if item = nil then
  114.             SysBeep(1)
  115.         else
  116.             TestDItemEnable := ControlHandle(item)^^.contrlHilite <> 255;
  117.     end;
  118.  
  119.     procedure HideDItem (theDialog: DialogPtr; itemNo: integer);
  120.         var
  121.             kind, realkind: integer;
  122.             item: ControlHandle;
  123.             box: Rect;
  124.     begin
  125.         GetDItem(theDialog, itemNo, kind, Handle(item), box);
  126.         if item = nil then
  127.             SysBeep(1)
  128.         else
  129.             begin
  130.                 realkind := BitAnd(kind, 127);
  131.                 case realkind of
  132.                     8, 16: {statText, editText}
  133.                         SetIText(handle(item), ''); {Nån bättre metod finns väl}
  134.                     0, 1, 2, 4, 5, 6: {button, checkbox, radio - men vad är 4?}
  135.                         HideControl(ControlHandle(item));
  136.                     otherwise
  137.                         SysBeep(1);
  138.                 end; {case}
  139.             end;
  140.     end;
  141.  
  142.     procedure ShowDItem (theDialog: DialogPtr; itemNo: integer);
  143.         var
  144.             kind, realkind: integer;
  145.             item: ControlHandle;
  146.             box: Rect;
  147.     begin
  148.         GetDItem(theDialog, itemNo, kind, Handle(item), box);
  149.         if item = nil then
  150.             SysBeep(1)
  151.         else
  152.             begin
  153.                 realkind := BitAnd(kind, 127);
  154.                 case realkind of
  155.                     8, 16: 
  156.                         ; {statText, editText}
  157.                     0, 1, 2, 4, 5, 6: {button, checkbox, radio - men vad är 4?}
  158.                         ShowControl(ControlHandle(item));
  159.                     otherwise
  160.                         SysBeep(1);
  161.                 end; {case}
  162.             end;
  163.     end;
  164.  
  165. {UpdDialog med rektangel i stället för region}
  166.     procedure UpdDialogRect (theDialog: DialogPtr; theRect: Rect);
  167.         var
  168.             rgn: RgnHandle;
  169.     begin
  170.         rgn := NewRgn;
  171.         RectRgn(rgn, theRect);
  172.         UpdtDialog(theDialog, rgn);
  173.         DisposeRgn(rgn);
  174.     end;
  175.  
  176. {Ett par triviala tvåradare - plus säkerhetskoll!}
  177. {Text till StatText/EditText}
  178.     procedure SetTextDItem (theDialog: DialogPtr; itemNo: integer; theString: Str255);
  179.         var
  180.             kind: integer;
  181.             item: ControlHandle;
  182.             box: Rect;
  183.     begin
  184.         GetDItem(theDialog, itemNo, kind, Handle(item), box);
  185.         if item = nil then
  186.             SysBeep(1)
  187.         else
  188.             begin
  189. {Kolla kind}
  190.                 kind := BitAnd(kind, 127);
  191.  
  192.                 case kind of
  193.                     8, 16: {statText, editText}
  194.                         SetIText(handle(item), theString);
  195.                     0, 1, 2, 4, 5, 6: {button, checkbox, radio - men vad är 4?}
  196.                         SetCTitle(item, theString);
  197.                     otherwise {Övriga har ingen text man kan sätta}
  198.                         SysBeep(1);
  199.                 end;{case}
  200.             end;
  201.     end;
  202.  
  203. {Texten från StatText/EditText}
  204.     function GetTextDItem (theDialog: DialogPtr; itemNo: integer): Str255;
  205.         var
  206.             kind: integer;
  207.             item: ControlHandle;
  208.             box: Rect;
  209.             tmpStr: Str255;
  210.     begin
  211.         GetDItem(theDialog, itemNo, kind, Handle(item), box);
  212.         if item = nil then
  213.             SysBeep(1)
  214.         else
  215.             begin
  216. {Kolla kind}
  217.                 kind := BitAnd(kind, 127);
  218.  
  219.                 tmpStr := '';
  220.                 case kind of
  221.                     8, 16: {statText, editText}
  222.                         GetIText(handle(item), tmpStr);
  223.                     0, 1, 2, 4, 5, 6: {button, checkbox, radio…?}
  224.                         GetCTitle(item, tmpStr);
  225.                     otherwise {Övriga har ingen text man kan sätta}
  226.                         SysBeep(1);
  227.                 end;{case}
  228.                 GetTextDItem := tmpStr;
  229.             end;
  230.     end;
  231.  
  232.  
  233. {Ett par triviala tvåradare - plus säkerhetskoll!}
  234. {Boolean till checkbox/radio}
  235.     procedure SetBooleanDItem (theDialog: DialogPtr; itemNo: integer; theBoolean: Boolean);
  236.         var
  237.             kind: integer;
  238.             item: ControlHandle;
  239.             box: Rect;
  240.     begin
  241.         GetDItem(theDialog, itemNo, kind, Handle(item), box);
  242.         if item = nil then
  243.             SysBeep(1)
  244.         else
  245.             begin
  246. {Kolla kind}
  247.                 kind := BitAnd(kind, 127);
  248.                 case kind of
  249.                     8, 16: {statText, editText}
  250.                         if theBoolean then
  251.                             SetIText(handle(item), 'true')
  252.                         else
  253.                             SetIText(handle(item), 'false');
  254.                     0, 1, 2, 4, 5, 6: {button, checkbox, radio - men vad är 4?}
  255.                         SetCtlValue(item, integer(theBoolean));
  256.                     otherwise {Övriga har ingen text man kan sätta}
  257.                         SysBeep(1);
  258.                 end;{case}
  259.             end;
  260.     end;
  261.  
  262. {Boolean från checkbox/radio}
  263.     function GetBooleanDItem (theDialog: DialogPtr; itemNo: integer): Boolean;
  264.         var
  265.             kind: integer;
  266.             item: ControlHandle;
  267.             box: Rect;
  268.     begin
  269.         GetDItem(theDialog, itemNo, kind, Handle(item), box);
  270.         if item = nil then
  271.             SysBeep(1)
  272.         else
  273.             begin
  274. {Kolla kind}
  275.                 kind := BitAnd(kind, 127);
  276.  
  277.                 GetBooleanDItem := false;
  278.                 case kind of
  279.                     0, 1, 2, 4, 5, 6: {button, checkbox, radio…?}
  280.                         GetBooleanDItem := boolean(GetCtlValue(item));
  281.                     otherwise {Övriga har inget värde}
  282.                         SysBeep(1);
  283.                 end;{case}
  284.             end;
  285.     end;
  286.  
  287.     procedure ToggleBooleanDItem (theDialog: DialogPtr; itemNo: integer);
  288.     begin
  289.         SetBooleanDItem(theDialog, itemNo, not GetBooleanDItem(theDialog, itemNo));
  290.     end;
  291.  
  292.     function GetDItemBox (theDialog: DialogPtr; itemNo: integer): Rect;
  293.         var
  294.             kind: integer;
  295.             item: ControlHandle;
  296.             box: Rect;
  297.             tmpStr: Str255;
  298.     begin
  299.         GetDItem(theDialog, itemNo, kind, Handle(item), box);
  300.         GetDItemBox := box;
  301.     end;
  302.  
  303. end.